#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QGridLayout>
#include <QMediaPlayer>
#include <QNetworkRequest>
#include <QVideoWidget>
#include <QVideoFrame>
#include <QLabel>
#include "myvideowidget.h"
#include "mybutton.h"
#include <QPushButton>

#define MAX_STREAM 9
#define SPLIT_NUM 3
#define SPLIT_OF_ONE 30

#define BACKGROUND_PATH "./img/background.png"
#define FULLSCREEN_PATH "./img/fullscreen.png"
#define LOGO_PATH "./img/Firefly.jpg"

void btquit()
{
	qApp->exit(0);
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Alterando o titulo do QMainWindow
    setWindowTitle("B2Open - RTSP e Streaming Video com Qt5 Widgets");
    
    QGridLayout *layout = new QGridLayout;
    Mybutton *bt[MAX_STREAM];
    Myvideowidget *_vw[MAX_STREAM];
    QMediaPlayer *_player[MAX_STREAM];
    QUrl url[MAX_STREAM];
    
    // 标题
    QLabel * label = new QLabel(this);
    label->setText("  中山天启智能科技有限公司  ");
    QFont ft;
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    ft.setPointSize(14);
    ft.setBold(100);
    label->setFont(ft);
    label->setAlignment(Qt::AlignHCenter);
    label->setPalette(pe);
    label->setStyleSheet("QLabel { background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 rgb(255,69,0),stop:0.4 rgb(218,165,32));border-radius: 3px; }");
    layout->addWidget(label, 0,0,1,SPLIT_NUM * SPLIT_OF_ONE);
    
    QPushButton * bt_logo = new QPushButton;
    bt_logo->setStyleSheet("QPushButton { border-image: url(./img/Firefly.jpg);border-radius: 3px; }"
    			   "QPushButton:hover { border-image: url(./img/Firefly_on.jpg);border-radius: 3px; }"
			   "QPushButton:pressed { border-image: url(./img/Firefly_on.jpg);border-radius: 3px; }");
    layout->addWidget(bt_logo, 0,0,1,1);
    bt_logo->setFixedSize(60,30);
    connect(bt_logo, &QAbstractButton::clicked, this,&btquit );

    QLabel * label_ip[MAX_STREAM];
    QIcon btn_icon;
    btn_icon.addFile(FULLSCREEN_PATH);
    
    for( int i = 0; i < MAX_STREAM ; i++) {
    
    	label_ip[i] = new QLabel;
    	_vw[i] = new Myvideowidget;
    	_player[i] = new QMediaPlayer;
    	bt[i] = new Mybutton(_vw[i]);
    	_player[i]->setVideoOutput(_vw[i]);
    	if (i > 4) 
	 url[i].setUrl("rtsp://admin:firefly123@168.168.2.96:554/av_stream/");
    	else
	 url[i].setUrl("rtsp://admin:firefly123@168.168.100.100:554/av_stream/");
    	label_ip[i]->setText("rtsp://admin:firefly123@168.168.2.96:554/av_stream/");
	bt[i]->setIcon(btn_icon);
	bt[i]->setFlat(true);
	_player[i]->setMedia(QNetworkRequest(url[i]));
	layout->addWidget(_vw[i],(i / SPLIT_NUM) * 2 + 2, ( i % SPLIT_NUM ) * SPLIT_OF_ONE,1,SPLIT_OF_ONE);
	layout->addWidget(bt[i],(i / SPLIT_NUM) * 2  + 1, ( i % SPLIT_NUM ) * SPLIT_OF_ONE,1,1);
	layout->addWidget(label_ip[i],(i / SPLIT_NUM) * 2  + 1, ( i % SPLIT_NUM ) * SPLIT_OF_ONE + 1,1,SPLIT_OF_ONE - 1);
    }
    
    // background
    QPalette pal;
    pal.setBrush(QPalette::Background,QBrush(QPixmap(BACKGROUND_PATH)));
    this->setPalette(pal);

    // Cria um QWudget que irá receber o layout com os VideoWidgets
    QWidget *win = new QWidget();
    win->setLayout(layout);
    setCentralWidget(win);


    for( int i = 0; i < MAX_STREAM ; i++) {
    	_player[i]->play();
    }
    
}

MainWindow::~MainWindow()
{
    delete ui;
}
